module.exports = function ($scope, $resource, $modal, toaster,FileUploader, date2str, str2date) {
  $scope.vm = {
    'date' : '',
    'options' : {
        format: "YYYY-MM-DD",
        locale : 'zh-cn'
    }
  }
  // 区县数组
  $scope.districtArr = [
    {'label' : '和平区', 'value' : '0'},
    {'label' : '沈河区', 'value' : '1'},
    {'label' : '皇姑区 ', 'value' : '2'},
    {'label' : '铁西区', 'value' : '3'},
    {'label' : '大东区', 'value' : '4'},
    {'label' : '东陵区', 'value' : '5'},
    {'label' : '于洪区', 'value' : '6'},
    {'label' : '苏家屯区', 'value' : '7'},
    {'label' : '沈北新区', 'value' : '8'},
    {'label' : '浑南新区', 'value' : '9'},
    {'label' : '辽中县', 'value' : '10'},
    {'label' : '康平县', 'value' : '11'},
    {'label' : '法库县', 'value' : '12'},
    {'label' : '新民市', 'value' : '13'}
  ]
  $scope.company = {};
  $scope.selection = function(aa){
    $scope.info.user_country = aa.value;
    $scope.info.user_country_name = aa.label;
  }
  $scope.companyinfo = function(){
    //无参数执行添加,查看当前机构申报状态
		$resource('/api/ac/goc/viewBaseService/getCompanyInfo', {}, {}).save({}, function(res){
      console.log(res);
      if (res.errcode === 0) {
				$scope.fromData.status = res.data.info;
				if(res.data.info == '0'){ // 直接注册
					// $scope.fromData.msg = '已经申报招徕奖励, 无需再次申报.';
				} else if(res.data.info == '1'){
          $scope.fromData.register = false;
					$scope.fromData.msg = '正在审核, 请耐心等待.';
				} else if(res.data.info == '2'){
          $scope.fromData.register = false;
					$scope.fromData.msg = '审核已通过, 可申报项目.';
				} else if(res.data.info == '3'){
          $scope.fromData.register = false;
					$scope.fromData.msg = '审核已驳回, 请重新申报项目.';
				}
      } else {
				$scope.fromData.register = false;
			}
    });
  }
	init();
	//查询当前机构是否已申报
	function init() {
    // $scope.company = {};
    $scope.info = {};
		$scope.fromData = {
			'register': true
		}
		$scope.companyinfo();
  }
  
  // 查询注册信息
  $scope.registerInfo = function(){
    $resource('/api/as/sc/office/getCompanyBaseInfo', {}, {}).save({}, function(res){
      if (res.errcode !== 0) {
        toaster.error({ title: "提示", body: res.errmsg});
        return;
      }
      console.log(res);
      $scope.company = res.data;
    });
  }
  $scope.registerInfo();
	// 文件上传
	var uploader = $scope.uploader = new FileUploader({
		url: 'https://txy.juyouhx.com/Api/Api/ObjectToOss?topdir=aa&selfdir=bb'
	});
	uploader.filters.push({
		name: 'imageFilter',
		fn: function (item /*{File|FileLikeObject}*/, options) {
			var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
			return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
		}
	});
	uploader.onSuccessItem = function (fileItem, response, status, headers) {
		$scope.info.view_code_img = response.savename;
	};

	$scope.uploader = uploader;
	// 文件上传
	var uploader1 = $scope.uploader1 = new FileUploader({
		url: 'https://txy.juyouhx.com/Api/Api/ObjectToOss?topdir=aa&selfdir=bb'
	});
	uploader1.filters.push({
		name: 'imageFilter',
		fn: function (item /*{File|FileLikeObject}*/, options) {
			var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
			return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
		}
	});
	uploader1.onSuccessItem = function (fileItem, response, status, headers) {
		$scope.company.license_img = response.savename;
	};

	$scope.uploader1 = uploader1;
	// 文件上传
	var uploader2 = $scope.uploader2 = new FileUploader({
		url: 'https://txy.juyouhx.com/Api/Api/ObjectToOss?topdir=aa&selfdir=bb'
	});
	uploader2.filters.push({
		name: 'imageFilter',
		fn: function (item /*{File|FileLikeObject}*/, options) {
			var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
			return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
		}
	});
	uploader2.onSuccessItem = function (fileItem, response, status, headers) {
		$scope.info.logo = response.savename;
	};

	$scope.uploader2 = uploader2;
	// 文件上传
	var uploader3 = $scope.uploader3 = new FileUploader({
		url: 'https://txy.juyouhx.com/Api/Api/ObjectToOss?topdir=aa&selfdir=bb'
	});
	uploader3.filters.push({
		name: 'imageFilter',
		fn: function (item /*{File|FileLikeObject}*/, options) {
			var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
			return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
		}
	});
	uploader3.onSuccessItem = function (fileItem, response, status, headers) {
		$scope.info.top_img = response.savename;
	};

	$scope.uploader3 = uploader3;

	//提交信息
	$scope.commit = function() {
    $scope.info.user_company_name = $scope.company.name;
    $scope.info.business_license_img = $scope.company.license_img;
    $scope.info.user_address = $scope.company.address;
    $scope.info.company_start_date = date2str($scope.vm.date._d);
    console.log($scope.info);
    
		// if(!checkInfo($scope.info)) return;
		$resource('/api/ac/goc/levelBaseService/createViewBase', {}, {}).save($scope.info, function(res){
        if (res.errcode === 0) {
            toaster.success({title: '', body: '提交成功'});
		        $scope.companyinfo();
        } else {
            toaster.error({title: '', body: res.errmsg});
        }
    });
  }
  // 重新申报
  $scope.reApply = function (){
    $scope.fromData.register = true;
  }
  
  //校验企业信息
	function checkInfo(info){
		if(!info.user_name){
			toaster.warning({title: '', body: '请填写负责人名称.'});
			return false;
		}
		if(!info.user_phone){
			toaster.warning({title: '', body: '请填写负责人手机号'});
			return false;
		}
		if(!/^1[3|5|7|8|9]\d{9}$/.test(info.user_phone)){
			toaster.warning({title: '', body: '手机号格式不正确'});
			return false;
		}
		if(!info.user_license){
			toaster.warning({title: '', body: '请填写执照编号.'});
			return false;
		}
		if(!info.user_license_img){
			toaster.warning({title: '', body: '请上传营业执照图片.'});
			return false;
		}
		return true;
	}

	

	


};